<?php

sleep(1);

function jsOff()
{
    $message['error'] = true;
    $message['message'] = "Please fill out all of the required fields!";

    return json_encode($message);
}

switch($_POST['action'])
{
    //Login Actions
    case 'login':
        if(!empty($_POST['email']) && !empty($_POST['password']))
        {
                $email = strip_tags($_POST['email']);
                $password = strip_tags(md5($_POST['password']));

                require_once 'classes/Login.php';
                $Login = new Login;
                if($message = $Login->checkUserLogin($email,$password))
                {
                        echo $message;
                }
        }
        else { jsOff(); }
    break;

    //Register Actions
    case 'register':
        if(!empty($_POST['companyName']) && !empty($_POST['contactName']) && !empty($_POST['emailReg']) && !empty($_POST['passwordReg']) && !empty($_POST['captcha']))
        {
            $companyName = strip_tags($_POST['companyName']);
            $contactName = strip_tags($_POST['contactName']);
            $email = strip_tags($_POST['emailReg']);
            $password = strip_tags(md5($_POST['passwordReg']));
            $captcha = strip_tags($_POST['captcha']);

            $userPassword = strip_tags($_POST['passwordReg']);

            require_once 'classes/Login.php';
            $Login = new Login;
            if($message = $Login->registerUser($companyName,$contactName,$email,$password,$captcha,$userPassword))
            {
                    echo $message;
            }
        }
        else { jsOff(); }
    break;

    //Forgot Password Actions
    case 'forgotPassword':
        if(!empty($_POST['forgotEmail']))
        {
                $email = strip_tags($_POST['forgotEmail']);
                require_once 'classes/Login.php';
                $Login = new Login;
                if($message = $Login->forgotPassword($email))
                {
                        echo $message;
                }
        }
        else { jsOff(); }
    break;

    //Renew Password Actions
    case 'changePassword':
        if(!empty($_POST['newPass']) && (!empty($_POST['newPass2'])) && (!empty($_POST['email'])) && (!empty($_POST['currentPass'])))
        {
                $currentPass = strip_tags(md5($_POST['currentPass']));
                $newPass = strip_tags(md5($_POST['newPass']));
                $newPass2 = strip_tags(md5($_POST['newPass2']));
                $email = strip_tags($_POST['email']);

                require_once 'classes/Login.php';
                $Login = new Login;
                if($message = $Login->changeUserPassword($email,$currentPass,$newPass,$newPass2))
                {
                        echo $message;
                }
        }
        else { jsOff(); }
    break;

    case 'renewPassword':
        if(!empty($_POST['newPass']) && (!empty($_POST['newPass2'])) && (!empty($_POST['email'])))
        {
                $newPass = strip_tags(md5($_POST['newPass']));
                $newPass2 = strip_tags(md5($_POST['newPass2']));
                $email = strip_tags($_POST['email']);

                require_once 'classes/Login.php';
                $Login = new Login;
                if($message = $Login->forgotChangeUserPassword($email,$newPass,$newPass2))
                {
                        echo $message;
                }


        }
        else { jsOff(); }
    break;
    
    //Submit Post Actions
    case 'submitPost':
                $employerID = $_POST['employerID'];
                $jobTitle = strip_tags($_POST['jobTitle']); 
                $jobDesc = strip_tags($_POST['jobDesc']); 
                $jobCatID = strip_tags($_POST['jobCat']);
                $salary = strip_tags($_POST['salary']); 
                $hideName = $_POST['hideName']; 
                $hideSalary = $_POST['hideSalary']; 
                $country = $_POST['country']; 
                $state1 = $_POST['state1']; 
                $state2 = $_POST['state2']; 
                $state3 = $_POST['state3']; 
                $state4 = $_POST['state4']; 
                $state5 = $_POST['state5']; 
                $city1 = strip_tags($_POST['city1']); 
                $city2 = strip_tags($_POST['city2']); 
                $city3 = strip_tags($_POST['city3']); 
                $city4 = strip_tags($_POST['city4']); 
                $city5 = strip_tags($_POST['city5']); 
                $submitArray = array($employerID, $jobTitle, $jobCatID, $jobDesc, $salary, $hideName, $hideSalary, $country, $state1, $state2, $state3, $state4, $state5, $city1, $city2, $city3, $city4, $city5);

                require_once 'classes/JobPost.php';
                $JobPost = new JobPost;
                echo $JobPost->submitPost($submitArray);
    break;

    //Get States Action
    case 'getStates':
        $countryID = $_POST['countryID'];
	require_once 'classes/JobPost.php';
	$JobPost = new JobPost;
        $states = $JobPost->getStates($countryID);
        echo $states;
    break;
}
?>